package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyChangShaQuanXuan;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeChangShaQuanXuan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * 安心-长沙全选25
 *
 * https://www.yuque.com/u33825202/xtwksg/pq9vsht5bw81oysd?singleDoc
 * 密码：rdac
 *
 * @author 李源聪
 */
@ApiPlan(productId = "56", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5ChangShaQuanXuan implements ApiOrgPhoneMd5ConvertHandler {

    private static final String APP_KEY = "c734d6af98554e26a11043b52df743c1";

    private static final String APP_SECRET = "65bbbc0ecea842d48e70776b454ebc85";
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        OrgDataExchangeChangShaQuanXuan data = new OrgDataExchangeChangShaQuanXuan();
        data.setAppKey(APP_KEY);
        data.setIsMask("1");
        data.setPhone(apiDataExchangeParameter.getPhoneMd5());
        String signStr = "appKey=" + APP_KEY + "&phone=" + apiDataExchangeParameter.getPhoneMd5() + APP_SECRET;
        data.setSign(DigestUtils.md5Hex(signStr).toUpperCase());
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("errCode"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyChangShaQuanXuan data = new OrgApplyChangShaQuanXuan();
        data.setAppKey(APP_KEY);

        // 是否加密，默认0 不加密；1加密
        data.setIsMask("0");

        //手机号
        data.setPhone(apiApplyParameter.getPhone());

        //客户姓名
        data.setName(apiApplyParameter.getUserName());

        //申请额度/意向金额 如：1000
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", "30000")
                    .when(apiApplyParameter.getLoanAmount(), "2", "50000")
                    .when(apiApplyParameter.getLoanAmount(), "3", "100000")
                    .when(apiApplyParameter.getLoanAmount(), "4", "200000")
                    .end();
            data.setApplyAmount(value);
        }
        // 年龄
        data.setAge(String.valueOf(apiApplyParameter.getAge()));
        data.setAge(apiApplyParameter.getAge());
        // 是否有保单 1=有 2=无
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "2")
                    .when(apiApplyParameter.getInsurancePolicy(), "2", "1")
                    .when(apiApplyParameter.getInsurancePolicy(), "3", "1")
                    .when(apiApplyParameter.getInsurancePolicy(), "4", "1")
                    .end();
            data.setWarranty(value);
        }
        //是否有公积金 1=有 2=无
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "2")
                    .when(apiApplyParameter.getProvidentFund(), "2", "1")
                    .when(apiApplyParameter.getProvidentFund(), "3", "1")
                    .when(apiApplyParameter.getProvidentFund(), "4", "1")
                    .end();
            data.setAccumulation(value);
        }
        // 有社保, 1=有 2=无
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "2")
                    .when(apiApplyParameter.getSocialSecurity(), "2", "1")
                    .when(apiApplyParameter.getSocialSecurity(), "3", "1")
                    .when(apiApplyParameter.getSocialSecurity(), "4", "1")
                    .end();
            data.setSocial(value);
        }
        // 是否有房 1=有 2=无
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "2")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "1")
                    .end();
            data.setHouse(value);
        }
        // 是否有车 1=有 2=无
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "2")
                    .when(apiApplyParameter.getCarInfo(), "2", "1")
                    .when(apiApplyParameter.getCarInfo(), "3", "1")
                    .when(apiApplyParameter.getCarInfo(), "4", "1")
                    .end();
            data.setCar(value);
        }
        // 代发 1=有 2=无
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "1")
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", "2")
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", "2")
                    .end();
            data.setIssuing(value);
        }
        // 企业纳税 1=有 2=无
        data.setBilling("2");
        // 企业开票 1=有 2=无
        data.setBilling("2");
        // 参数拼接字符串appKey=asd&phone=13012345678，尾部再添加appSecret，进行MD5加密：md5(appKey={appKey}&phone={phone}+{appSecret})，加密后字符串转化为大写。
        // 注：签名中的phone值，随是否加密改变，如果isMask为1，请使用aes加密后的手机号
        String signStr = "appKey=" + APP_KEY + "&phone=" + apiApplyParameter.getPhone() + APP_SECRET;
        data.setSign(DigestUtils.md5Hex(signStr).toUpperCase());
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("errCode"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
